<template>
  <div id="add-blog">
    <h2>添加博客</h2>
    <form v-if="!submmited">
      <label>博客标题</label>
      <input type="text" v-model="blog.title" required />

      <label>博客内容</label>
      <textarea v-model="blog.content"></textarea>

      <label>博客分类</label>
      <div id="checkboxes">
        <label>Vue.js</label>
        <input type="checkbox" value="Vue.js" v-model="blog.categories">
        <label>Node.js</label>
        <input type="checkbox" value="Node.js" v-model="blog.categories">
        <label>React.js</label>
        <input type="checkbox" value="React.js" v-model="blog.categories">
        <label>Angular.js</label>
        <input type="checkbox" value="Angular.js" v-model="blog.categories">
      </div>

      <label>博客作者</label>
      <select v-model="blog.author">
        <option v-for="(item,index) in authors" :key="index" :value="item">{{item}}</option>
      </select>

      <button @click.prevent="post">添加博客</button>
    </form>

    <h3 v-if="submmited">您的博客发布成功！</h3>
    <div id="preview">
      <h3>博客总览</h3>
      <p>博客标题：{{blog.title}}</p>
      <p>博客内容：</p>
      <p>{{blog.content}}</p>
      <p>博客分类：</p>
      <p>
        <ul>
          <li v-for="(item,index) in blog.categories" :key="index">{{item}} </li>
        </ul>
      </p>
      <p>博客作者：{{blog.author}}</p>
    </div>
  </div>
</template>

<script>
import Qs from 'qs'

export default {
  name: 'add-blog',
  data() {
    return {
      blog:{
        title:"",
        content:"",
        categories:[],
        author:""
      },
      authors:["张三","李四","王五"],
      submmited:false
    }
  },
  methods:{
    post:function(){
      this.$axios.post("http://127.0.0.1:8000/app/addBlog",
        Qs.stringify({params:JSON.stringify(this.blog)}))
        .then((data) => {
          console.log(data);
          this.submmited = true;
        });
    }
  }
}
</script>


<style scoped>
#add-blog *{
  box-sizing: border-box;
}

#add-blog{
  margin: 20px auto;
  max-width: 600px;
  padding: 20px;
}

lable{
  display: block;
  margin: 20px 0 10px;
}

input[type="text"],textarea,select{
  display: block;
  width: 100%;
  padding: 8px;
}

textarea{
  height: 200px;
}

#checkboxs lable{
  display: inline-block;
  margin-top: 0;
}
#checkboxs input{
  display: inline-block;
  margin-right: 10px;
}

button{
  display: block;
  margin: 20px 0;
  background: crimson;
  color: #ffffff;
  border: 0;
  padding: 14px;
  border-radius: 4px;
  font-size: 18px;
  cursor: pointer;
}

#preview{
  padding: 10px 20px;
  border: 1px dotted #cccccc;
  margin: 30px 0;
}
h3{
  margin-top: 10px;
}
</style>
